//
//  ListDiyViewController.m
//  Mahjong
//
//  Created by KING on 16/9/11.
//  Copyright © 2016年 swift. All rights reserved.
//

#import "ListDiyViewController.h"
#import "Header.h"


#import "ListNumberTableViewCell.h"
#import "MineListAdressTableViewCell.h"
#import "MineListEraTableViewCell.h"
#import "MineListPayTableViewCell.h"
#import "LIstDiyEditTableViewCell.h"
#import "ListDiyDEditTableViewCell.h"
#import "ListDiyTextTableViewCell.h"
#import "ListDiyMoreTextTableViewCell.h"
#import "MineListNumberTableViewCell.h"

@interface ListDiyViewController ()
<UITableViewDelegate,UITableViewDataSource,editDelegate>
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, assign) NSInteger number;

@end

@implementation ListDiyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREENH_HEIGHT-80) style:UITableViewStylePlain];
    [self.view addSubview:self.myTableView];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.myTableView registerNib:[UINib nibWithNibName:@"ListNumberTableViewCell" bundle:nil] forCellReuseIdentifier:@"numberCell"];
    [self.myTableView registerNib:[UINib nibWithNibName:@"MineListAdressTableViewCell" bundle:nil] forCellReuseIdentifier:@"adressCell"];
    [self.myTableView registerNib:[UINib nibWithNibName:@"MineListEraTableViewCell" bundle:nil] forCellReuseIdentifier:@"eraCell"];

    [self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.myTableView registerNib:[UINib nibWithNibName:@"LIstDiyEditTableViewCell" bundle:nil] forCellReuseIdentifier:@"editCell"];
    [self.myTableView registerNib:[UINib nibWithNibName:@"ListDiyDEditTableViewCell" bundle:nil] forCellReuseIdentifier:@"deditCell"];
    [self.myTableView registerNib:[UINib nibWithNibName:@"ListDiyTextTableViewCell" bundle:nil] forCellReuseIdentifier:@"textCell"];
    [self.myTableView registerNib:[UINib nibWithNibName:@"ListDiyMoreTextTableViewCell" bundle:nil] forCellReuseIdentifier:@"mtextCell"];
    [self.myTableView registerNib:[UINib nibWithNibName:@"MineListPayTableViewCell" bundle:nil] forCellReuseIdentifier:@"payCell"];
    [self.myTableView registerNib:[UINib nibWithNibName:@"ListDiyTextTableViewCell" bundle:nil] forCellReuseIdentifier:@"btextCell"];
    [self.myTableView registerNib:[UINib nibWithNibName:@"MineListNumberTableViewCell" bundle:nil] forCellReuseIdentifier:@"numCell"];

    
    [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:@"ListDiyEdit"];
    [self createButton];
}
-(void)createButton{
    
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREENH_HEIGHT-106, SCREEN_WIDTH, 40)];
    backView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:backView];
    
   
    
    UIButton *_disButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _disButton.frame = CGRectMake(SCREEN_WIDTH- 200, 5, 90, 35);
    _disButton.backgroundColor = [UIColor orangeColor];
    [_disButton setTitle:@"取消订单" forState:UIControlStateNormal];
    [backView addSubview:_disButton];
    
    
    UIButton *_okButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _okButton.frame = CGRectMake(SCREEN_WIDTH- 100, 5, 90, 35);
    _okButton.backgroundColor = [UIColor redColor];
    [_okButton setTitle:@"付款" forState:UIControlStateNormal];
    [backView addSubview:_okButton];
    
    if (self.diyNum == 200) {
        [_disButton setTitle:@"联系买家" forState:UIControlStateNormal];
        [_okButton setTitle:@"报价" forState:UIControlStateNormal];
    }
    if (self.diyNum == 201) {
        [_disButton setTitle:@"联系买家" forState:UIControlStateNormal];
        [_okButton setTitle:@"已报价" forState:UIControlStateNormal];
    }if (self.diyNum == 202) {
        [_disButton setTitle:@"联系买家" forState:UIControlStateNormal];
        [_okButton setTitle:@"未完成" forState:UIControlStateNormal];
        
    }if (self.diyNum == 203) {
        [_disButton setTitle:@"联系买家" forState:UIControlStateNormal];
        [_okButton setTitle:@"未评价" forState:UIControlStateNormal];
    }
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 2;
    }
    if (section == 1) {
        return 1;
    }
    if (section == 2) {
        return 4;
    }
    return 6;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            ListNumberTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"numberCell"];
            return cell;
        }
        else{
        MineListAdressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"adressCell"];
        return cell;
        }
    }
    if (indexPath.section == 1) {
        MineListEraTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"eraCell"];
        return cell;
    }
    if (indexPath.section == 2) {
        if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"ListDiyEdit"] isEqualToString:@"0"]) {
            LIstDiyEditTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"editCell"];
            return cell;
        }
        else{
            ListDiyDEditTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"deditCell"];
            cell.myDelegate = self;
            return cell;
        }
    }
    else{
        if (indexPath.row == 0) {
            ListDiyTextTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"textCell"];
            cell.titleLabel.text = @"设计费";
            return cell;
        }
        if (indexPath.row == 1) {
            ListDiyMoreTextTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mtextCell"];
            return cell;
        }if (indexPath.row == 2) {
            MineListPayTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"payCell"];
            cell.titleLabel.text = @"预计完成时间";
            return cell;
        }if (indexPath.row == 3) {
            ListDiyTextTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"btextCell"];
            cell.titleLabel.text = @"运费(送货)";
            return cell;
        }if (indexPath.row == 4) {
            MineListNumberTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"numCell"];
            cell.titleLabel.text = @"总价合计";
            cell.conLabel.text = @"¥0";
            return cell;
        }
        else{
            MineListNumberTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"numCell"];
            cell.titleLabel.text = @"需付定金";
            cell.conLabel.text = @"¥0";
            return cell;
        }
    }
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        if (indexPath.row ==1) {
            return 80;
        }
    }
    return 40;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 2) {
        return 30;
    }
    return 10;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 2) {
        
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 88, 25)];
    titleLabel.text = @"定制无聊";
    [titleView addSubview:titleLabel];
    
    UIButton *ediButton = [UIButton buttonWithType:UIButtonTypeCustom];
    ediButton.frame = CGRectMake(SCREEN_WIDTH - 100, 0, 44, 22);
    
    [ediButton setTitle:@"编辑" forState:UIControlStateNormal];

    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"ListDiyEdit"] isEqualToString:@"1"]) {
        [ediButton setTitle:@"保存" forState:UIControlStateNormal];

    }
    ediButton.backgroundColor = [UIColor blueColor];
    [ediButton addTarget:self action:@selector(ediAction:) forControlEvents:UIControlEventTouchUpInside];
    [titleView addSubview:ediButton];
    
    UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
    addButton.frame = CGRectMake(SCREEN_WIDTH - 50, 0, 44, 22);
    [addButton setTitle:@"添加" forState:UIControlStateNormal];
    addButton.backgroundColor = [UIColor blueColor];
    [addButton addTarget:self action:@selector(addAction:) forControlEvents:UIControlEventTouchUpInside];
    [titleView addSubview:addButton];
    
    

    return titleView;
    }
    return nil;
}
-(void)ediAction:(UIButton *)bt{
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"ListDiyEdit"] isEqualToString:@"0"]) {
        [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"ListDiyEdit"];
        [self.myTableView reloadData];
    }else{
        [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:@"ListDiyEdit"];
        [self.myTableView reloadData];
    }

}

-(void)remo:(NSInteger)num{
    NSLog(@"删除");
}
-(void)addAction:(UIButton *)bt{
    NSLog(@"123");
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
